#include "pub_pop_get_item.h"
#include "ui_pub_pop_get_item.h"
#include <QMessageBox>
#include <QtDebug>
#include <QSqlQueryModel>
#include <QModelIndex>
#include "pub_var.h"
#include <QThread>

Pub_pop_get_item::Pub_pop_get_item(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Pub_pop_get_item)
{
    ui->setupUi(this);

    connect(&tm, &QTimer::timeout, this, &Pub_pop_get_item::tmot);

    retValue = "";
}

Pub_pop_get_item::~Pub_pop_get_item()
{
    delete ui;
}

void Pub_pop_get_item::on_buttonBox_accepted()
{
    QString ret;

    for(int i = 0; i< columns; i++)
    {
        ret += ui->tvList->currentIndex().siblingAtColumn(i).data(0).toString();
        ret += "^";
    }
    retValue = ret;
//qDebug()<<" retValue is :"<< ret;
    //emit retStr( ret );
}

QString Pub_pop_get_item::retStr()
{
    return retValue;
}

void Pub_pop_get_item::on_buttonBox_rejected()
{
    retValue = "";
    //emit retStr( "" );
}

void Pub_pop_get_item::tmot()
{
    tm.stop();

    int width = ui->tvList->width();

    for(int i = 0; i < columns; i++)
    {
        double v = pub::getStr(columnWidths, "^", i+1).toDouble();
        ui->tvList->setColumnWidth(i, width * v);
    }

}

void Pub_pop_get_item::on_tvList_doubleClicked(const QModelIndex &index)
{
    Q_UNUSED(index)
    close();
    on_buttonBox_accepted();

}

void Pub_pop_get_item::setDisplay(QSqlQueryModel & md, QRect rct, QString tip, QString sep)
{
    Q_UNUSED(tip)

    this->setGeometry(rct);
    this->setFixedSize(rct.width(),rct.height());
    columnWidths = sep;
    columns = md.columnCount();

    ui->tvList->setModel(&md);
    QModelIndex mdidx = ui->tvList->indexAt(QPoint(0,0));
    ui->tvList->setCurrentIndex(mdidx);
    ui->tvList->setFocus();

    tm.start(20);
}
